﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace XNA_TD.Menue
{
    public class VerlorenMenue : GUI
    {
        double startTime;
        Texture2D texture;
        Color color;
        Vector2 pos;
        Vector2 zeitPos;
        string verloren;
        string zeit;
        public VerlorenMenue()
        {
            startTime = gameTime.TotalGameTime.TotalMilliseconds;
            texture = new Texture2D(graphics.GraphicsDevice, 1, 1);
            color = new Color(255, 255, 255, 0);
            texture.SetData(new Color[] { color });
            gameSpeed = 0;

            var toUnRegister = new List<GUI>();

            foreach (var gui in gameManager.getRenderList(Priority.GUI))
            {
                var t = gui as GUI;
                if (t != null && gui as VerlorenMenue == null)
                {
                    toUnRegister.Add(t);
                }
            }

            foreach (var t in toUnRegister)
            {
                UnRegister(t);
            }

            verloren = "VERLOREN";
            zeit = "Du hast " + (int)(inGameTime / 1000 / 60) + " Minuten Ueberlebt";

            Resize();
        }

        public override void Draw()
        {
            if (color.A != 255)
            {
                color.A = (byte)Math.Min(255, (gameTime.TotalGameTime.TotalMilliseconds - startTime) / 4);
                texture.SetData(new Color[] { color });
            }
            spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.NonPremultiplied);
            spriteBatch.Draw(texture, size, Color.White);
            if (color.A == 255)
            {
                spriteBatch.DrawString(DebugInfo.Instance.debugFont, verloren, pos, Color.Black);
                spriteBatch.DrawString(DebugInfo.Instance.debugFont, zeit, zeitPos, Color.Black);
            }
            spriteBatch.End();
        }

        public override void Resize()
        {
            size = new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height);
            pos = new Vector2((int)(graphics.GraphicsDevice.Viewport.Width / 2 - DebugInfo.Instance.debugFont.MeasureString(verloren).X / 2), (int)(graphics.GraphicsDevice.Viewport.Height / 2 - 10));
            zeitPos = new Vector2((int)(graphics.GraphicsDevice.Viewport.Width / 2 - DebugInfo.Instance.debugFont.MeasureString(zeit).X / 2), (int)(graphics.GraphicsDevice.Viewport.Height / 2 + 10));
        }

        public override void MouseClick()
        {
            gameManager.Exit();
        }
    }
}
